#include "NetworkServer.h"

namespace mp_net
{
	NetworkServer::NetworkServer()
		: NetworkSession()
	{
		m_acceptor = NULL;
	}
	NetworkServer::~NetworkServer()
	{
		//Lock for cleanup, 
		//NetworkSession calls Unlock
		while(!TryLock()){;}

		StopOperatingThread();

		delete m_acceptor;
	}

	void NetworkServer::Initialize()
	{
		//- ..' A try lock
		while(!TryLock()){;}
		
		//- ..' Do Work


		//- ..' Unlock
		Unlock();
	}

	void NetworkServer::Update()
	{
		//-
	}

	void NetworkServer::Uninitialize()
	{
		//-
	}

	void NetworkServer::Start()
	{
		while(!TryLock()){;}
		
		m_ioService.run();

		//Setup server acceptor
		m_acceptor = new tcp::acceptor(
			m_ioService, tcp::endpoint(tcp::v4(),m_port));

		//Setup socket
		m_socket = socket_ptr(new tcp::socket(m_ioService));

		//Start async accept
		m_acceptor->async_accept(*m_socket, 
			boost::bind(&NetworkServer::NewConnection, this,
			 boost::asio::placeholders::error) );
		
		m_ioService.reset();

		StartOperatingThread();
		Unlock();
	}

	void NetworkServer::NewConnection(const boost::system::error_code& error)
	{
		if (!error)
		{
			
		}
		int x = 1;
	}

	void NetworkServer::NetThreadProc()
	{
		while (!m_stopRequested)
		{
			int y = 1;
		}
	}
}